import { message } from 'antd';
import { history } from 'umi';
import { addRole,fetchRole,updateRole,menusAllList,queryMenuByRole} from './service';

const Model = {
  namespace: 'cubeRole',
  state: {
    menuList:[],
    menuIdList:[]
  },
  effects: {
    *add({ payload }, { call }) {
      let result=yield call(addRole, payload);
      if(result.code===0){
        message.success('提交成功');
        history.push(`/auth/role`)
      }else{
        message.error(result.message);
      }
    },
    *update({ payload }, { call }) {
      let result=yield call(updateRole, payload);
      if(result.code===0){
        message.success('提交成功');
        history.push(`/auth/role`)
      }else{
        message.error(result.message);
      }
    },
    *fetchRole({ payload }, { call,put }) {
      let result=yield call(fetchRole, payload);
      if(result.code===0){
        yield put({
          type: 'common',
          payload: {
            currentSite:result.data
          }
        });
      }else{
        message.error(result.message);
      }
    },
    *menusAllList({ payload }, { call,put }) {
      let result=yield call(menusAllList, payload);
      yield put({
        type: 'common',
        payload: {
          menuList:result.children
        }
      });
    },
    *queryMenuByRole({ payload }, { call,put }) {
      let result=yield call(queryMenuByRole, payload);
      if(result.code===0){
        return result.data.buttonIdList.contact(result.data.menuIdList);
      }else{
        message.error(result.message);
      }
    },
  },
  reducers: {
    common(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};
export default Model;
